//
// Created by sally on 24-11-10.
//

#include "src/UdpService.h"
#include <iostream>
#include <thread>
#include <chrono>

int main() {
    // 1. 创建 UdpService 实例，设定 IP 和端口
    std::string ip = "172.21.93.12";      // 本地回环地址
    uint16_t port = 12345;             // 端口号
    uint8_t threadNum = 4;             // 线程数量，用于运行多个事件循环

    // 2. 创建并启动 UdpService
    droneswarm::UdpService udpServer(ip, port, threadNum);
    udpServer.start();

    // 3. 发送一条消息测试服务器
    std::string targetIp = "255.255.255.255"; // 目标地址，与服务器相同
    uint16_t targetPort = 12345;        // 目标端口，与服务器端口相同
    std::string message = "Hello from test program!";
    std::string domain_name = "www.baidu.com";
    //等待服务器初始化好
    std::this_thread::sleep_for(std::chrono::seconds(1));
    bool sendResult = udpServer.send_message(targetIp, targetPort, message);
    if (sendResult) {
        // std::cout << "Message sent successfully!" << std::endl;
    } else {
        // std::cerr << "Failed to send message." << std::endl;
    }

     udpServer.send_messageByDomainName(domain_name, targetPort, message);
    // 4. 等待一段时间，确保服务器能够接收并处理消息
    std::this_thread::sleep_for(std::chrono::seconds(3));

    // 5. 停止 UdpService
    udpServer.stop();

    return 0;
}
